package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //新增文章分类
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success();
    }

    //获取分类
    /**
     * 获取类别列表
     *
     * 通过GET请求获取所有类别信息
     *
     * @return 返回类别列表的Result对象
     */
    @GetMapping
    public Result<List<Category>> list(){
        // 调用服务层方法获取类别列表
        List<Category> cs = categoryService.list();
        // 返回成功结果，包含类别列表
        return Result.success(cs);
    }


    //获取文章分类详情
    @GetMapping("/detail")
    public  Result<Category> detail(@RequestParam  Integer id){
      Category c =  categoryService.findById(id);
      return Result.success(c);
    }


    //更新文章分类
    @PutMapping
    public  Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }

    //删除文章分类
    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }




}
